package com.tcy.bigevent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tcy.bigevent.mapper.ArticleMapper;
import com.tcy.bigevent.pojo.Article;
import com.tcy.bigevent.pojo.PageBean;
import com.tcy.bigevent.service.ArticleService;
import com.tcy.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> getAllArticles() {
        return null;
    }

    @Override
    public void add(Article article) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer)map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }

    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }
    @Override
    public Article getById(Integer id) {
        return articleMapper.getById(id);
    }
    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer)claims.get("id");
        //1、创建PageBean对象
        PageBean<Article> pageBean = new PageBean<>();

        //2、开启分页查询PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3、调用mapper
        List<Article> items = articleMapper.list(userId,categoryId,state);
        Page<Article> page = (Page<Article>) items;
        pageBean.setItems(page.getResult());
        pageBean.setTotal(page.getTotal());

        return pageBean;
    }
}
